package com.core.common.manger.storage.config;

import com.core.common.manger.MangerProperties;
import com.core.common.manger.storage.*;
import io.minio.MinioClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(MangerProperties.class)
public class StorageAutoConfiguration {

    private final MangerProperties properties;

    public StorageAutoConfiguration(MangerProperties properties) {
        this.properties = properties;
    }

    @Bean
    public IStorageService storageService() {

        String active = this.properties.getStorage().getActive();
        IStorageService service;

        if (active.equals("local")) {
            service=localStorage();
        } else if (active.equals("aliyun")) {
            service=aliyunStorage();
        } else if (active.equals("tencent") ){
            service=tencentStorage();
        } else if (active.equals("qiniu") ){
            service=qiniuStorage();
        } else if (active.equals("minio") ){
            service=minioStorage();
        } else {
            throw new RuntimeException("当前存储模式 " + active + " 不支持");
        }

        return service;
    }

 //   @Bean
    public StorageLocal localStorage() {
        StorageLocal localStorage = new StorageLocal();
        MangerProperties.StorageProperties.Local local = this.properties.getStorage().getLocal();
        localStorage.setAddress(local.getAddress());
        localStorage.setStoragePath(local.getStoragePath());
        return localStorage;
    }

   // @Bean
    public StorageAliyun aliyunStorage() {
        StorageAliyun aliyunStorage = new StorageAliyun();
        MangerProperties.StorageProperties.Aliyun aliyun = this.properties.getStorage().getAliyun();
        aliyunStorage.setAccessKeyId(aliyun.getAccessKeyId());
        aliyunStorage.setAccessKeySecret(aliyun.getAccessKeySecret());
        aliyunStorage.setBucketName(aliyun.getBucketName());
        aliyunStorage.setEndpoint(aliyun.getEndpoint());
        aliyunStorage.setAddress(aliyun.getAddress());
        return aliyunStorage;
    }

   // @Bean
    public StorageTencent tencentStorage() {
        StorageTencent tencentStorage = new StorageTencent();
        MangerProperties.StorageProperties.Tencent tencent = this.properties.getStorage().getTencent();
        tencentStorage.setSecretId(tencent.getSecretId());
        tencentStorage.setSecretKey(tencent.getSecretKey());
        tencentStorage.setBucketName(tencent.getBucketName());
        tencentStorage.setRegion(tencent.getRegion());
        tencentStorage.setAddress(tencent.getAddress());
        return tencentStorage;
    }

//    @Bean
    public StorageQiniu qiniuStorage() {
        StorageQiniu qiniuStorage = new StorageQiniu();
        MangerProperties.StorageProperties.Qiniu qiniu = this.properties.getStorage().getQiniu();
        qiniuStorage.setAccessKey(qiniu.getAccessKey());
        qiniuStorage.setSecretKey(qiniu.getSecretKey());
        qiniuStorage.setBucketName(qiniu.getBucketName());
        qiniuStorage.setEndpoint(qiniu.getEndpoint());
        qiniuStorage.setAddress(qiniu.getAddress());
        return qiniuStorage;
    }
    //    @Bean
    public StorageMinio minioStorage() {
        StorageMinio minioStorage = new StorageMinio();
        MangerProperties.StorageProperties.Minio minio = this.properties.getStorage().getMinio();
        minioStorage.setAccessKey(minio.getAccessKey());
        minioStorage.setSecretKey(minio.getSecretKey());
        minioStorage.setBucketName(minio.getBucketName());
        minioStorage.setEndpoint(minio.getEndpoint());
        minioStorage.setAddress(minio.getAddress());
        return minioStorage;
    }

    @Bean
   // @ConditionalOnBean(StorageMinio.class)
    public MinioClient getMinioClient() {
        MangerProperties.StorageProperties.Minio minio = this.properties.getStorage().getMinio();
        return MinioClient.builder().endpoint(minio.getEndpoint())
                .credentials(minio.getAccessKey(), minio.getSecretKey())
                .build();
    }
}
